﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Common;

namespace com.globalwave.system
{
    /// <summary>
    /// user 的摘要说明。
    /// </summary>
    public partial class OrganizationPage : Common.BasePage
    {
        public OrganizationPage():base() {

            entityName = "Organization";
        }
        
        public void list() {

            NameValueCollection criterion = getCriterion();

            DataSet ds = OrganizationService.getInstance().query(criterion, pageIndex, pageSize);
            
            renderListXML(ds) ;
        }

        public void create()
        {

            NameValueCollection organization = getOrganization();

            int organization_id =
                OrganizationService.getInstance().create(organization, getPrivilege_ids());

            organization.Set("organization_id", organization_id.ToString() );

            renderSuccessCreateMessage(organization);
        }

        public void update()
        {

            NameValueCollection organization = getOrganization();

            OrganizationService.getInstance().update(organization, getPrivilege_ids());

            renderSuccessUpdateMessage(organization);
            
        }

        public void delete()
        {
            string[] organization_ids = this.getOrganization_ids();

            if (organization_ids == null)
            {
                OrganizationService.getInstance().delete(getOrganization());
            }
            else
            {
                OrganizationService.getInstance().deleteAll(organization_ids);
            }

            renderSuccessDeleteMessage();
            
        }

        public void userBelongTo()
        {
            DataSet ds = OrganizationService.getInstance().userBelongTo(Request.Params.Get("user_id"));

            renderListXML(ds);

        }

        private NameValueCollection getCriterion()
        {

            return U.filter(Request.Params, "organizationCriterion")[0];
        }

        private NameValueCollection getOrganization()
        {
            NameValueCollection organization = U.filter(Request.Params, "organization")[0];
            if ("".Equals(organization.Get("pro_organization_id")))
            {
                organization.Set("pro_organization_id", null);
            }
            return organization;
        }

        private string[] getOrganization_ids()
        {
            return Request.Params.GetValues("organization_ids");
        }

        private string[] getPrivilege_ids()
        {
            return Request.Params.GetValues("privilege_ids");
        }

        private static Hashtable privilegeMap = null;

        override protected Hashtable getPrivilegeMap()
        {
            if (privilegeMap == null)
            {
                privilegeMap = new Hashtable();

                privilegeMap.Add("login", (short)-1);
                privilegeMap.Add("list", (short)-1);
                privilegeMap.Add("create", (short)-1);
                privilegeMap.Add("update", (short)-1);
                privilegeMap.Add("delete", (short)-1);
                privilegeMap.Add("userBelongTo", (short)-1);
            }

            return privilegeMap;
        }
    }
}
